#include <libmodbus/include/modbus.h>
#include <thread>
#include "../../config.hpp"


namespace dcs {


    class rtu_slave_t {
        log_handler_t log_handler_;
        std::int32_t id_ = 0;
        modbus_t* ctx_ = nullptr;
        modbus_mapping_t* buffer_ = nullptr;
        std::unique_ptr<std::jthread> recv_thr_;


    public:
        rtu_slave_t(const log_handler_t&);
        ~rtu_slave_t();

        rtu_slave_t(const rtu_slave_t&) = delete;
        rtu_slave_t& operator=(const rtu_slave_t&) = delete;


        void set_id(std::int32_t id) { id_ = id; }
        bool start(const std::string& device, int baudrate, char parity, int data_bit, int stop_bit, std::uint32_t registers_num);
        void stop();
        bool handle();

        void update(std::time_t, std::uint16_t, const std::vector<float>&);
    };

}